package com.trade.tradebusiness.trade.base;


import com.trade.core.entity.TradeBaseContext;
import com.trade.core.exception.TradeBaseException;
import com.trade.core.message.TradeObjectRestResponse;
import com.trade.core.trade.TradeProcessorStepAbstract;
import com.trade.tradebusiness.enums.BaseTradeProcessorStepEnums;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author sven.wang
 * @version 2022/1/6 15:26
 * @Description: 获取并校验用户id
 */
@Service
@Slf4j
public class GetAndCheckUserIdStep extends TradeProcessorStepAbstract<Object,TradeBaseContext> {
    @Override
    public String step() {
        return BaseTradeProcessorStepEnums.GET_AND_CHECK_USER_ID.name();
    }


    @Override
    public void checkParam(Object param) {
        //nothing to do
    }

    @Override
    public TradeObjectRestResponse<TradeBaseContext> doProcess(Object param) {
        TradeBaseContext tradeBaseContext = new TradeBaseContext();
        String userId = getAndCheckUserId();
        tradeBaseContext.setUserId(userId);
        return TradeObjectRestResponse.success(tradeBaseContext);
    }

    @Override
    public void checkOutParam(TradeBaseContext outParam) {
        if(StringUtils.isBlank(outParam.getUserId())){
            throw new TradeBaseException("步骤返回数据异常: 用户id不存在");
        }
    }

    /**
     * 获取并校验商户后台用户id
     *
     * @return 用户id
     */
    public String getAndCheckUserId() {
        //手动模拟获取用户id
        String userId = "123123";
        if (StringUtils.isBlank(userId)) {
            throw new TradeBaseException("用户id不存在,用户未登录");
        }
        return userId;
    }
}
